package com.example.comic.mvp.view

import android.content.Intent
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.ActivityOptionsCompat
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.MenuItem
import android.view.View
import com.example.comic.R
import com.example.comic.adpter.BookAdapter
import com.example.comic.mvp.model.Book
import com.example.comic.mvp.ppresenter.BookPresenter
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_sort.*
import kotlinx.android.synthetic.main.item_book.view.*

import org.jetbrains.anko.async
import org.jetbrains.anko.find
import org.jetbrains.anko.uiThread

class SortActivity : AppCompatActivity() {
    companion object {
        const val INTENT_LINK = "link"
        const val INTENT_COVER = "cover"
        const val INTENT_TITLE = "title"
    }

    lateinit var link: String
    private lateinit var cover: String
    lateinit var title: String
    private var mData = ArrayList<Book>()

    private lateinit var adapter: BookAdapter
    lateinit var recyclerView: RecyclerView
    lateinit var swipeRefresh: SwipeRefreshLayout
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sort)
        initView()
    }

    private fun initView() {
        link = intent.getStringExtra(INTENT_LINK)
        cover = intent.getStringExtra(INTENT_COVER)
        title = intent.getStringExtra(INTENT_TITLE)
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        collapsingToolbar.title
        if (!cover.isEmpty())
            Picasso.get().load(cover).into(imageView)
        swipeRefresh = find(R.id.swipeRefresh)
        swipeRefresh.setOnRefreshListener { load() }
        recyclerView = find(R.id.recyclerView)
        recyclerView.layoutManager = GridLayoutManager(this, 2)
        adapter = BookAdapter { itemView: View, i: Int -> jumpToChapter(itemView, i) }
        recyclerView.adapter = adapter
    }

    override fun onResume() {
        super.onResume()
        swipeRefresh.isRefreshing = true
        load()
    }

    private fun jumpToChapter(itemView: View, position: Int) {
        val intent = Intent(this, ChapterActivity::class.java)
        intent.putExtra(ChapterActivity.INTENT_LINK, mData[position].link)
        intent.putExtra(ChapterActivity.INTENT_COVER, mData[position].cover)
        intent.putExtra(ChapterActivity.INTENT_TITLE, mData[position].title)
        intent.putExtra(ChapterActivity.INTENT_CATEGORY, mData[position].category)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val options: ActivityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(this, itemView.ivCover, "imageView")
            startActivity(intent, options.toBundle())
        } else {
            startActivity(intent)
        }

    }

    private fun load() {
        async {
            val data = BookPresenter().sortContent(link)
            uiThread {
                swipeRefresh.isRefreshing = false
                mData = data
                adapter.refreshData(mData)
            }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        return when (item!!.itemId) {
            android.R.id.home -> {
                onBackPressed()
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }

    override fun onBackPressed() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            finishAfterTransition()
        else
            super.onBackPressed()
    }
}
